<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>倒计时界面</title>
</head>
<body>
    <div id="txt"></div>
    <script>
        var set = new Date("2022.2.14 00:00:00");
        var timer = null;
        var txt = document.getElementById("txt");
        function countDown() {
            var now = new Date();
            var ms = set - now;
            if (ms > 0) {
                var d = Math.floor(ms / 1000 / 60 / 60 / 24);
                    d >= 10 ? d : "0" + d;
                var h = Math.floor((ms - d * 1000 * 60 * 60 * 24) / 1000 / 60 / 60);
                    h >= 10 ? h : "0" + h;
                var m = Math.floor((ms - d * 1000 * 60 * 60 * 24 - h * 1000 * 60 * 60) / 1000 / 60);
                    m >= 10 ? m : "0" + m;
                var s = Math.floor((ms - d * 1000 * 60 * 60 * 24 - h * 1000 * 60 * 60 - m * 1000 * 60) / 1000);
                    s >= 10 ? s : "0" + s;
                    console.log(s);
                    txt.innerHTML = "倒计时" + d + "天" + h + "小时" + m + "分" + s + "秒";
            }else{
                    txt.innerHTML = "情人节到啦";
                    clearInterval(timer);
                    timer = null;
            }
        }
            window.onload = function(){
                timer = setInterval(countDown, 1000);
            }
    </script>
</body>
</html>
